
<template>
    <div class="order-details not-print">
        <div class="order-bg" @click="hideModel"></div>
        <div class="order-container cloth-info-main" loading="load">
            <div class="order-title">{{dialogTitle}}</div>
            <img src="../../../assets/img/close-icon.png" alt="" class="close-model" @click="hideModel">

            <!-- 弹窗内容 -->
            <div
                :class="['order-state',status[row.order_status].divClass ]"
                 v-if="dialogTitle != '确认下单'">
                <span>订单状态：</span>
                <span :class="[status[row.order_status].spanClass ]">
                    {{status[row.order_status].title }}
                </span>
                <span v-if="row.is_myself == '1'" class="is-myself">自发单</span>
            </div>
            <div class="order-scroll">
                <!-- 申领单信息 -->
                <div class="application-order">
                    <div class="application-order-title">申领单信息</div>
                    <div class="application-order-info">
                        <div class="order-info" v-for="(v,i) in info" :key="i">
                            <span class="order-info-title">{{v.title}}</span>
                            <span class="order-info-text">{{v.value}}</span>
                        </div>
                    </div>
                    <!-- 申领详细 -->
                    <div class="apply-details">
                        <div class="apply-details-title">申领详细 <span>（总数：{{detail.order_total}} / {{detail.order_status == 1 ? '--' : detail.check_total}} / {{detail.order_status == 3 ? detail.send_total : '--'}}）</span></div>
                        <div class="order-list cloth-table">
                            <div class="el-table  el-table--border"  style="width: 100%">
                                <table  cellspacing="0" cellpadding="0" border="0" class="el-table__header" style="width: 100%">
                                    <colgroup>
                                        <col width="90">
                                        <col width="90">
                                        <col width="80">
                                        <col  v-if="$root.$data.cloth_des">
                                        <col>
                                        <col v-if="dialogTitle != '确认下单'">
                                        <col v-if="dialogTitle != '确认下单'">
                                        <col width="90">
                                        <col width="90">
                                    </colgroup>
                                    <thead>
                                    <tr class="thead">
                                        <th class="is-center is-leaf"><div class="cell">核定病床</div></th>
                                        <th class="is-center is-leaf"><div class="cell">织物类型</div></th>
                                        <th class="is-center is-leaf"><div class="cell">尺码</div></th>
                                        <th class="is-center is-leaf" v-if="$root.$data.cloth_des"><div class="cell">备注</div></th>
                                        <th class="is-center is-leaf"><div class="cell qty-number">下单数量</div></th>
                                        <th class="is-center is-leaf" v-if="dialogTitle != '确认下单'"><div class="cell check-number">确认数量</div></th>
                                        <th class="is-center is-leaf" v-if="dialogTitle != '确认下单'"><div class="cell send-number">实际送达</div></th>
                                        <th class="is-center is-leaf"><div class="cell">合计</div></th>
                                        <th class="is-center is-leaf"><div class="cell">最高限量</div></th>
                                    </tr>
                                    </thead>
                                </table>
                                <div class="el-table  el-table--border"  style="width: 100%; height: calc(100% - 40px); margin-top: 0" >
                                    <table  cellspacing="0" cellpadding="0" border="0" class="el-table__body" style="width: 100%;">
                                        <colgroup>
                                            <col width="90">
                                            <col width="90">
                                            <col width="80">
                                            <col v-if="$root.$data.cloth_des">
                                            <col>
                                            <col v-if="dialogTitle != '确认下单'">
                                            <col v-if="dialogTitle != '确认下单'">
                                            <col width="90">
                                            <col width="90">
                                        </colgroup>
                                        <tbody>

                                        <template v-for="(weaving,windex) in detail.weaving" :keys="windex">
                                            <tr v-for="(size,sindex) in weaving.sizes" :key="weaving.wname+size.wsize">
                                                <td class="is-center" v-if="windex == 0 && sindex == 0" :rowspan="detail.rowl"><div class="cell">{{detail.bed_num}}</div></td>
                                                <td class="is-center"  v-if=" sindex == 0" :rowspan="weaving.sizes.length"><div class="cell">
                                                    {{weaving.wname}}
                                                </div></td>
                                                <td class="is-center"><div class="cell">
                                                    {{size.wsize}}
                                                </div></td>

                                                <td class="is-center point"  v-if="$root.$data.cloth_des" @click="handleChangeDes(size,windex,sindex)"><div class="cell des-cell">
                                                    
                                                    <span>{{size.wsize_des}}</span>
                                                    <i class="el-icon-edit" title="点击修改" v-if="dialogTitle != '确认下单' && detail.order_status == '1'"></i>
                                                </div></td>

                                                <td class="is-center point" @click="handleChangeNumber(size,windex,sindex)">
                                                    <div class="cell qty-cell">
                                                        
                                                        <div v-if="dialogTitle == '确认下单'" class="qty-number" >{{size.wsize_qty==''?0:size.wsize_qty}}</div>
                                                        <template v-else>
                                                            <el-popover
                                                                placement="top-start"
                                                                title="历史数量"
                                                                trigger="hover"
                                                                v-if="size.qty_log.length > 0"
                                                            >
                                                                    <div
                                                                        v-for="(logs,lind) in size.qty_log" :key="lind"
                                                                    >{{lind+1}}、{{logs.des1 ?logs.des1: logs.des}}</div>
                                                                <div slot="reference" class="qty-number" @mouseover="handleHoverSize(size,windex,sindex,'1')">{{size.wsize_qty==''?0:size.wsize_qty}}</div>
                                                            </el-popover>
                                                            <span v-else class="qty-number" @mouseover="handleHoverSize(size,windex,sindex,'1')">{{size.wsize_qty==''?0:size.wsize_qty}}</span>
                                                        </template>
                                                        <i class="el-icon-edit" title="点击修改" v-if="dialogTitle != '确认下单' && detail.order_status == '1'"></i>
                                                    </div>
                                                </td>

                                                <td class="is-center" v-if="dialogTitle != '确认下单'">
                                                    <div :class="['cell',  detail.order_status != 1 && size.check != size.wsize_qty && (!size.wsize_qty && size.check == 0) ? 'red ' : 'check-number',{'gray':detail.order_status == 1}]">
                                                        <el-popover
                                                            placement="top-start"
                                                            title="历史数量"
                                                            trigger="hover" 
                                                            v-if="size.check_log.length > 0"
                                                            >
                                                                <div
                                                                    v-for="(logs,lind) in size.check_log" :key="lind"
                                                                >{{lind+1}}、{{logs.des1 ?logs.des1: logs.des}}</div>
                                                            <div slot="reference"> {{size.check ? size.check : 0}}</div>
                                                        </el-popover>
                                                        <div  v-if="size.check_log.length == 0" @mouseover="handleHoverSize(size,windex,sindex,'2')">{{size.check ? size.check : 0}}</div>
                                                       
                                                    </div>
                                                </td>
                                                <td class="is-center" v-if="dialogTitle != '确认下单'">
                                                    <div :class="['cell', size.send != size.check && detail.order_status == 3 ? 'red':'send-number']">
                                                        <el-popover
                                                            placement="top-start"
                                                            title="历史数量"
                                                            trigger="hover"
                                                            v-if="size.send_log.length > 0"
                                                            >
                                                                <div
                                                                    v-for="(logs,lind) in size.send_log" :key="lind"
                                                                >{{lind+1}}、{{logs.des1 ?logs.des1: logs.des}}</div>
                                                            <div slot="reference" > {{size.wsize_send ? size.wsize_send : 0}}</div>
                                                        </el-popover>
                                                        <div  v-if="size.send_log.length == 0" @mouseover="handleHoverSize(size,windex,sindex,'3')">
                                                            {{detail.order_status == 3 && size.wsize_send ? size.wsize_send : '--'}}
                                                        </div>
                                                    </div>
                                                </td>

                                                <td class="is-center"
                                                    v-if="sindex == 0" :rowspan="weaving.sizes.length">
                                                    <div :class="['cell total',dialogTitle == '确认下单' && Number(weaving.wname_total) > Number(weaving.wname_max) ? 'red':'qty-number']">
                                                        {{weaving.wname_total ? weaving.wname_total : 0}}
                                                    </div>
                                                    <div v-if="dialogTitle != '确认下单'" class="cell total check-number">
                                                        {{detail.order_status == 1 ? '--' :weaving.check_total}}
                                                    </div>
                                                    <div v-if="dialogTitle != '确认下单'" class="cell total send-number">
                                                        {{detail.order_status == 3 ?weaving.send_total : '--'}}
                                                    </div>
                                                </td>
                                                <td class="is-center"  v-if="sindex == 0" :rowspan="weaving.sizes.length"><div class="cell number">
                                                    {{weaving.wname_max}}
                                                </div></td>
                                            </tr>
                                        </template>


                                        </tbody>
                                        <tbody>
                                            <tr>
                                                <td class="is-center"><div class="cell">合计</div></td>
                                                <td class="is-center"><div class="cell">--</div></td>
                                                <td class="is-center"><div class="cell">--</div></td>
                                                <td class="is-center" v-if="$root.$data.cloth_des"><div class="cell">--</div></td>
                                                <td class="is-center"><div class="cell qty-number">{{detail.order_total}}</div></td>
                                                <td class="is-center" v-if="dialogTitle != '确认下单'"><div class="cell check-number">{{detail.order_status == 1 ? '--' : detail.check_total}}</div></td>
                                                <td class="is-center" v-if="dialogTitle != '确认下单'"><div class="cell send-number">{{detail.order_status == 3 ? detail.send_total : '--'}}</div></td>
                                                <td class="is-center"><div class="cell">--</div></td>
                                                <td class="is-center"><div class="cell">--</div></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 订单进度 -->
                <div class="order-progress" v-if="dialogTitle != '确认下单'">
                    <div class="order-progress-title">轨迹</div>
                    <div class="progress-lis">
                        <div class="progress-item" v-for="(v,i) in row.orderlog" :key="i">
                            <div class="progress-time">{{v.ctime}}</div>
                            <div class="progress-info">
                                <div class="info-name">{{v.uname}}</div>
                                <div class="info-fun">
                                    <img :src="icon[v.ukind] " alt="">
                                    <span>{{v.des}}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 确认按钮 -->
            <div class="btn-box">
                <div class="btn-box-con" v-if="dialogTitle == '确认下单'">
                    <div class="cancel-btn" @click="hideModel">取消</div>
                    <el-button class="submit-btn" :loading="isSubmit" @click="submitOrder">
                        <span v-if="!isSubmit">确认提交</span>
                        <span v-else>提交中……</span>
                    </el-button>
                </div>
                <div class="btn-box-con" v-else>
                    <el-button class="submit-btn"  @click="handlePrint">打印</el-button>
                </div>
            </div>


        </div>

        <el-dialog
            :modal="false"
            :visible.sync="visible"
            :title="'修改'+row.weaving[inputs.pindex].wname+inputs.row.wsize+'的'+inputs.title"
            width="340px"
            top="30%"
        >
            <div v-if="visible">
                <el-input
                    v-if="inputs.title=='备注' "
                    placeholder="请输入尺码备注"
                    v-model="inputs.wsize_des"
                    autofocus
                ></el-input>
                <el-input-number
                    v-else
                    v-model="inputs.wsize_qty"
                    :step="inputs.wsize_qty != 0 && Number(inputs.wsize_qty) < Number(inputs.wsize_min) ? Number(inputs.wsize_qty): inputs.wsize_min"
                    :min="0"
                    class="alter-num"
                ></el-input-number>
            </div>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSureAlter">确 认</el-button>
                <el-button @click="visible = false">取 消</el-button>
            </div>
        </el-dialog>



    </div>
</template>

<script>
import {
    clothTimeAxios,
    sizeLogAxios,
    clothModAxios,
    clothModLogAxios
} from '../../../server/getData'
import {
    timeFormat,
    getCountDay
} from '../../../common/today'

export default {
    mame:'OrderDetail',
    props:{
        row:{
            type:Object
        },
        dialogTitle:'',
        status: Object,
    },
    
    data(){
        return{
            load:false,
            info:[],
            detail:{},
            isSubmit:false,
            inputs:{
                title:'备注',
                wsize_qty:0,
                wsize_des:'',
                pindex:0,
                row:{}
            },
            visible:false,
            times:'',
            order_status:{
                "1":{title:"待确认",spanClass:"confirm",divClass:"confirm-state",tdClass:"font-gray"},
                "2":{title:"待送达",spanClass:"complete",divClass:"complete-state",tdClass:"font-gray"},
                "3":{title:"已送达",spanClass:"done",divClass:"done-state",tdClass:""}
            },
            icon:{
                "1":"/static/img/icon_wechat.png",
                "2":"/static/img/progress-icon.png",
                "0":"/static/img/icon_app.png",
            }
        }
    },
    
    created(){
        this.handleInit()
    },

    methods:{
        handleInit(){
            this.load = true
            this.detail = this.row
            if(this.dialogTitle != '确认下单'){

                this.detail['rowl'] = 0
                this.detail.weaving.forEach(v=>{
                    this.detail.rowl += v.sizes.length
                    
                    // v.sizes.forEach(size=>{
                        // size['log'] = []
                        // size["check"] = size.wsize_check
                        // size["send"] = size.send
                        // if(!size.wsize_check){
                        //     size.check = size.wsize_qty
                        //     size.send = size.wsize_qty
                        // }else if(!size.wsize_send){
                        //     size.send = size.wsize_check
                        // }
                        // sizeLogAxios({
                        //     id:this.row.id,
                        //     wname:v.wname,
                        //     wsize:size.wsize,
                        //     status_kind: '1',
                        // }).then(res=>{
                        //     size.log = res.data
                        // })
                    // })
                })
                
                clothTimeAxios().then(res=>{
                    this.times = timeFormat()+' '+res.data.stime
                })
                this.info = [
                    {title:'申领科室：',key:'address',value:this.row.addr},
                    {title:'下单人：',key:'uname',value:this.row.uname},
                    {title:'下单时间：',key:'ctime',value:this.row.ctime},
                    {title:'申领单号：',key:'des',value:this.row.order_sn},
                    {title:'预定申领日期：',key:'bookingtime',value:this.row.order_time_txt},
                ]
                if(this.$root.$data.cloth_des){
                    this.info.push({title:'订单备注：',key:'des',value:this.row.des})
                }
                setTimeout(v=>{
                    this.load = false
                    this.$forceUpdate()
                },1000)
            }else{
                this.info = [
                    {title:'下单人：',key:'foundername/creationtime',value:Cookies.get("nickname")},
                    {title:'申领科室：',key:'address',value:this.row.addr},
                    {title:'预定申领日期：',key:'bookingtime',value:this.row.order_time},
                    {title:'订单备注：',key:'des',value:this.row.des}
                ]
                this.load = false
            }
        },
        handleHoverSize(item,pindex,index,opt){
            if(opt == 1 && item.qty_log.length == 0){
                this.getClothSizeLogAxios(pindex,index,opt,"qty_log") 
            }else if(opt == 2 && item.check_log.length == 0){
                this.getClothSizeLogAxios(pindex,index,opt,"check_log") 
            }else if(opt == 3 && item.send_log.length == 0 && this.detail.order_status == 3){
                this.getClothSizeLogAxios(pindex,index,opt,"send_log") 
            }
        },
        getClothSizeLogAxios(pindex,index,opt,key){
            sizeLogAxios({
                id: this.row.id,
                wname: this.detail.weaving[pindex].wname,
                wsize: this.detail.weaving[pindex].sizes[index].wsize,
                status_kind: opt,
            }).then(res=>{
                if(res.success){
                    this.detail.weaving[pindex].sizes[index][key] = res.data
                }
                
            })
        },
        submitOrder(){ // 确定订单
            if(this.isSubmit){return}
            this.isSubmit = true
            this.$emit('onSure',_=>{this.isSubmit = false})
        },

        handleChangeDes(row,pindex,index){
            if(!this.handleEstimate() && this.dialogTitle == '确认下单' || this.detail.order_status != '1') return
            this.inputs = {
                title: '备注',
                wsize_qty: 0,
                wsize_des: row.wsize_des,
                pindex: pindex,
                index: index,
                row: row
            }
            this.visible = true
        },
        handleChangeNumber(row,pindex,index){
            if((!this.handleEstimate() && this.dialogTitle == '确认下单') || this.detail.order_status != '1') return
            this.inputs = {
                title: '下单数量',
                wsize_qty: Number(row.wsize_qty),
                wsize_min: Number(row.wsize_min),
                wsize_des: '',
                pindex: pindex,
                index: index,
                row: JSON.parse(JSON.stringify(row))
            }
            this.visible = true
        },
        handleEstimate(){
            let nowD = new Date(timeFormat()).getTime()
            let od = new Date(timeFormat(this.row.order_time)).getTime()
            if(nowD+24*60*60*1000 ==  od ){
                //需比较有没有过截止时间
                if(new Date().getTime() < new Date(this.times).getTime()){
                    return true
                }
                return false
            }else if(nowD>=od){
                //比较申领日期是否今天以及以前的
                return false
            }
            return true
        },
        handleSureAlter(){
            let des =  this.detail.weaving[this.inputs.pindex].wname+this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize, des1 = ''
            if(this.inputs.title=='下单数量'){
                //判断是否有修改
                if( this.inputs.row.wsize_qty == this.inputs.wsize_qty){
                    throwErrorTips('','下单数量没有被修改，请修改再提交！')
                    return
                }
                //修改的时候是否需要 判断总数是否为0？
//                if(this.inputs.wsize_qty == 0){
//                    let total = 0
//                    this.detail.weaving.forEach((v,i)=>{
//                        v.sizes.forEach((size,index)=>{
//                            if(i == this.inputs.pindex && index = this.inputs.index){
//
//                            }else {
//                                total +=  Number(size.wsize_qty)
//                            }
//                        })
//                    })
//                    if(total == 0){
//
//                    }
//                }
                this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize_qty = this.inputs.wsize_qty.toString()
                let t = 0
                this.detail.weaving[this.inputs.pindex].sizes.forEach(v=>{
                    t += Number(v.wsize_qty)
                })
                this.detail.weaving[this.inputs.pindex].wname_total = t.toString()
                des = '把'+des+this.inputs.title+' '+ this.inputs.row.wsize_qty+' 修改为 '+this.inputs.wsize_qty
                des1 = '科室用户 '+Cookies.get("nickname")+' 修改数量为：'+this.inputs.wsize_qty
            }else {
                //判断是否有修改
                let olddes = this.inputs.row.wsize_des
                if( olddes == this.inputs.wsize_des){
                    throwErrorTips('','备注没有被修改，请修改再提交！')
                    return
                }
                this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize_des = this.inputs.wsize_des
                if(olddes){
                    des = '把'+des+this.inputs.title+' '+olddes+' 修改为 '+this.inputs.wsize_des
                }else {
                    des += '添加备注：' +this.inputs.wsize_des
                }

            }

            clothModAxios({
                data:this.detail,
                log:{
                    id: this.detail.id,
                    des: des,
                    des1: des1,
                    wname:this.detail.weaving[this.inputs.pindex].wname,
                    wsize:this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize,
                    status_kind: '1',
                }    
            }).then(res=>{
                // clothModLogAxios({
                //     data:{
                //         id: this.detail.id,
                //         des: des,
                //         des1: des1,
                //         wname:this.detail.weaving[this.inputs.pindex].wname,
                //         wsize:this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize,
                //         status_kind: '1',
                //     },
                //     log:
                // }).then()
                this.visible = false
                this.load = true
                
                setTimeout(_=>{
                    this.$emit('onrefresh')
                    setTimeout(()=>{
                        this.load = false
                        this.$forceUpdate()
                        this.handleInit()
                    },200)
                },400)
            },res=>{
                //恢复？
                if(this.inputs.title=='下单数量'){
                    this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize_qty =  this.inputs.row.wsize_qty.toString()
                    let t = 0
                    this.detail.weaving[this.inputs.pindex].sizes.forEach(v=>{
                        t += Number(v.wsize_qty)
                    })
                    this.detail.weaving[this.inputs.pindex].wname_total = t.toString()
                }else {
                    this.detail.weaving[this.inputs.pindex].sizes[this.inputs.index].wsize_des = this.inputs.row.wsize_des
                }
            })



        },

        hideModel(){
            this.$emit('hideModel',false)
        },
        handlePrint(){
            this.$root.$data.cloth_print = {
                show:true,
                row:this.detail
            }
//            this.$emit('onprint',this.detail)
        }
    }
}
</script>

<style lang="less">
@import '../../../assets/less/clothDetail.less';
</style>



